<?php
	FLEA::loadClass('FLEA_Db_TableDataGateway');
	class Model_Albums extends FLEA_Db_TableDataGateway
	{
		var $tableName = 'tb_album';
		var $primaryKey = 'album_id';
		var $hasMany = array(
			array(
				'tableClass' => 'Model_Songs',
				'foreignKey' => 'album_id',
				'mappingName' => 'songs',
			),
		);
		var $belongsTo = array(
			array(
            'tableClass' => 'Model_Labels',
            'foreignKey' => 'label_id',
            'mappingName' => 'labels',
			),

			array(
            'tableClass' => 'Model_Artists',
            'foreignKey' => 'artist_id',
            'mappingName' => 'artists',
			),

		);

		var $manyToMany = array(
                array(
            'tableClass' => 'Model_Styles',
            'mappingName' => 'style',
            'joinTable' => 'tb_complex',
			),
        );


		function _getAlbums($artistId)	{
			$conditions = 'artist_id='.$artistId;
			$albums = $this->findAll($conditions, 'album_id ASC');
			
			return $albums;
		}

		function _getAlbum($albumId){
			$conditions = 'album_id='.$albumId;
			$album = $this->find($conditions);
			
			return $album;
		}

		function uploadPhoto($album_id = null,$thumbfile, $photofile)
		{
			if (!isset($album_id)) {
				$publish_date = date('Y-m-d H:i:s');
			}else{
				$album = FLEA::getSingleton('Model_Albums');
				$row = $album->find($album_id);
				$publish_date = $row['publish_date'];
			}
			if (isset($thumbfile) && isset($photofile)) {
				//$prefix = date('Y-m');
				
				$photoDir = FLEA::getAppInf('uploadDir') . DS . 'photos';
				$thumbDir = FLEA::getAppInf('uploadDir') . DS . 'thumbs';
				// 创建需要的目录
				FLEA::loadHelper('file');
				
				mkdirs($photoDir);
				mkdirs($thumbDir);

				$photoName  = md5(time() . $photofile->getTmpName());
				$thumbFilename = $photoName .'_130.' . $thumbfile->getExt();
				$photoFilename = $photoName .'_100.' . $photofile->getExt();
				$oriFilename = $photoName .'_240.' . $photofile->getExt();
				//dump($photoFilename);
				// 生成缩略图
				FLEA::loadHelper('image');
				$thumbImage =& FLEA_Helper_Image::createFromFile($thumbfile->getTmpName(), $thumbfile->getExt());
				$photoImage =& FLEA_Helper_Image::createFromFile($photofile->getTmpName(), $photofile->getExt());
				$photoImage->crop(FLEA::getAppInf('thumbWidth'), FLEA::getAppInf('thumbHeight'), true, true);

				$thumbImage->saveAsJpeg($thumbDir . DS . $thumbFilename);
				$thumbImage->destory();
				$photoImage->saveAsJpeg($photoDir . DS . $photoFilename);
				$photoImage->destory();

				// 保存原始相片
				$photofile->move($photoDir . DS . $oriFilename);

				$thumbFilename = str_replace(DS, '/', $thumbFilename);
				$photoFilename = str_replace(DS, '/', $photoFilename);
				$oriFilename = str_replace(DS, '/', $oriFilename);

			} else {	
				$album = FLEA::getSingleton('Model_Albums');
				$row = $album->find($album_id);
				$thumbFilename = $row['album_thumb'];
				$photoFilename = $row['album_photo'];
				$oriFilename = $row['album_ori'];
				
				//dump($thumbFilename);
			}
			// 添加数据库记录
			
			$data = array(				
				'album_id' => $album_id,
				'album_name' => htmlspecialchars($_POST['album_name']),
				'artist_id' => $_POST['artist_id'],			
				'album_thumb' => $thumbFilename,
				'album_photo' => $photoFilename,
				'album_ori' => $oriFilename,
				'release_date' => $_POST['release_date'],	
				'label_id' => $_POST['label_id'],
				'album_intro' => $_POST['album_intro'],
				'album_status' => 1,
				'publish_date' =>$publish_date,
				'update_date' => date('Y-m-d H:i:s'),				
				'album_content' => $_POST['album_content'],
				'style' => $_POST['style_id'],

			);
			//dump($data);
			$tableAlbums = FLEA::getSingleton('Model_Albums');
			return $tableAlbums->save($data);			
		}
	}
?>